SET versn=6.0.635
TITLE Lectrode's Shutdown Timer v%versn%

REM -----Settings-----
REM Action to perform (overriden if INPT_ACTION=1)
REM a/s/r/l/b/h/g/p/k (See below for what each does)
SET actn=s

REM Name of Program to start/end (overriden if INPT_ACTION=1)
REM Ignored if actn is not p or e
SET ProgramName=sample.exe

REM Set timer, number of seconds (overriden if INPT_TIME=1)
SET /A delaysecs=60*90

REM Custom Program names (allows you to select these from list if
REM you choose to Start or Kill program)
SET CUSTOMPROGRAM1=notepad.exe
SET CUSTOMPROGRAM2=
SET CUSTOMPROGRAM3=
SET CUSTOMPROGRAM4=

SET INPT_TIME=1
SET INPT_ACTION=1
SET TIMER_REFRESH_RATE=1
SET DEBUGGING=0


REM -----------------------------------------------
REM -             MAIN SCRIPT START               -
REM -----------------------------------------------

:TOP
IF %DEBUGGING%==1 SET DEBUGN=::
CALL :JUST_HEADER

REM Initialize
%DEBUGN%MODE CON COLS=60 LINES=30
PROMPT ^>
%DEBUGN%ECHO Initializing...
SETLOCAL ENABLEDELAYEDEXPANSION
SET NUMHOURS=0
SET NUMMINUTES=0
SET NUMSECS=0


CALL :GET_HOURS
CALL :GET_MINUTES
REM CALL :GET_SECONDS
CALL :SET_DELAYSECS

CALL :GET_ACTION
CALL :PROCESS_ACTION
CALL :WAITSECS %delaysecs%
CALL :PERFORM_ACTION
GOTO :TOP



REM -----------------------------------------------
REM -                  METHODS                    -
REM -----------------------------------------------


:STATS
%DEBUGN%REM ---------------------PROGRAM STATUS-----------------------
%DEBUGN%@ECHO OFF
%DEBUGN%SET SHOWaction=%action%                                                                                         !
%DEBUGN%SET SHOWTimerStatus=%TimerStatus%                                                                               !
%DEBUGN%CLS
%DEBUGN%						ECHO  **********************************************************
%DEBUGN%						ECHO  *                                                        *
%DEBUGN%						ECHO  *  ******   Lectrode's Shutdown Timer v%versn%  ******   *
%DEBUGN%						ECHO  *                                                        *
%DEBUGN%						ECHO  * http://code.google.com/p/useful-scripts-by-electrodexs *
%DEBUGN%						ECHO  *                                                        *
%DEBUGN%						ECHO  **********************************************************
%DEBUGN%IF "%DEBUGGING%"=="1"	ECHO  *DEBUGGING ONLY! Set DEBUGN to 0 to enable functionality *
%DEBUGN%						ECHO  *                                                        *
%DEBUGN%						ECHO  * Action on Timer Up: %SHOWaction:~0,35%*
%DEBUGN%						ECHO  *                                                        *
%DEBUGN%						ECHO  *                                                        *
%DEBUGN%						ECHO  *        %SHOWTimerStatus:~0,48%*
%DEBUGN%						ECHO  *                                                        *
%DEBUGN%						ECHO  **********************************************************
GOTO :EOF
%DEBUGN%REM ---------------------END PROGRAM STATUS----------------------

:SLEEP
%DEBUGN%REM ------------------------PROGRAM SLEEP-------------------------
%DEBUGN%REM SLEEP
%DEBUGN%REM Program sleeps for %1 seconds
IF "%1"=="" SET pN=3
IF NOT "%1"=="" SET pN=%1
PING -n 2 -w 1000 127.0.0.1>NUL
PING -n %pN% -w 1000 127.0.0.1>NUL
GOTO :EOF
%DEBUGN%REM ------------------------END PROGRAM SLEEP---------------------

:JUST_HEADER
%DEBUGN%REM ---------------------PROGRAM STATUS-----------------------
%DEBUGN%@ECHO OFF
%DEBUGN%CLS
%DEBUGN%						ECHO  **********************************************************
%DEBUGN%						ECHO  *                                                        *
%DEBUGN%						ECHO  *  ******   Lectrode's Shutdown Timer v%versn%  ******   *
%DEBUGN%						ECHO  *                                                        *
%DEBUGN%						ECHO  * http://code.google.com/p/useful-scripts-by-electrodexs *
%DEBUGN%						ECHO  *                                                        *
%DEBUGN%						ECHO  **********************************************************
%DEBUGN%						ECHO.
%DEBUGN%						ECHO.
GOTO :EOF


:StrLength
::StrLength(retVal,string)
set #=%2%
set length=0
:stringLengthLoop
if defined # (set #=%#:~1%&set /A length += 1&goto stringLengthLoop)
set "%~1=%length%"
GOTO :EOF


:CALC_NAMES
SET C1=0
SET C2=0
SET C3=0
SET C4=0
SET SPACES=25
IF NOT "%CUSTOMPROGRAM1%"=="" CALL :StrLength C1 %CUSTOMPROGRAM1%
IF NOT "%CUSTOMPROGRAM2%"=="" CALL :StrLength C2 %CUSTOMPROGRAM2%
IF NOT "%CUSTOMPROGRAM3%"=="" CALL :StrLength C3 %CUSTOMPROGRAM3%
IF NOT "%CUSTOMPROGRAM4%"=="" CALL :StrLength C4 %CUSTOMPROGRAM4%
SET LAST=%C1%
IF %C2% GEQ %LAST% SET LAST=%C2%
IF %C3% GEQ %LAST% SET LAST=%C3%
IF %C4% GEQ %LAST% SET LAST=%C4%
IF %LAST% GEQ %SPACES% SET /A SPACES=LAST+2
GOTO :EOF

:SHOW_CUSTOM_PROGRAMS
SET SPACER=                                  !
SET SHOWCUSTOMPROGRAM1=%CUSTOMPROGRAM1%%SPACER%
SET SHOWCUSTOMPROGRAM2=%CUSTOMPROGRAM2%%SPACER%
SET SHOWCUSTOMPROGRAM3=%CUSTOMPROGRAM3%%SPACER%
SET SHOWCUSTOMPROGRAM4=%CUSTOMPROGRAM4%%SPACER%
%DEBUGN%IF NOT "%CUSTOMPROGRAM1%"=="" ECHO !SHOWCUSTOMPROGRAM1:~0,%SPACES%![1]
%DEBUGN%IF NOT "%CUSTOMPROGRAM2%"=="" ECHO !SHOWCUSTOMPROGRAM2:~0,%SPACES%![2]
%DEBUGN%IF NOT "%CUSTOMPROGRAM3%"=="" ECHO !SHOWCUSTOMPROGRAM3:~0,%SPACES%![3]
%DEBUGN%IF NOT "%CUSTOMPROGRAM4%"=="" ECHO !SHOWCUSTOMPROGRAM4:~0,%SPACES%![4]
GOTO :EOF


:GET_HOURS
IF %INPT_TIME%==0 GOTO :EOF
CALL :JUST_HEADER
%DEBUGN%ECHO Please enter the amount of time to wait:
%DEBUGN%ECHO (Enter nothing for 0)
%DEBUGN%ECHO.
SET usrInput=0
SET /P usrInput=Number of hours: 
CALL :TEST_USRINPUT_NUM %usrInput% usrInput
IF %usrInput%==INVALID GOTO :GET_HOURS
SET NUMHOURS=%usrInput%
GOTO :EOF

:GET_MINUTES
IF %INPT_TIME%==0 GOTO :EOF
SET usrInput=0
SET /P usrInput=Number of minutes: 
CALL :TEST_USRINPUT_NUM %usrInput% usrInput
IF %usrInput%==INVALID GOTO :GET_MINUTES
SET NUMMINUTES=%usrInput%
GOTO :EOF

:GET_SECONDS
IF %INPT_TIME%==0 GOTO :EOF
SET usrInput=0
SET /P usrInput=Number of seconds: 
CALL :TEST_USRINPUT_NUM %usrInput% usrInput
IF %usrInput%==INVALID GOTO :GET_SECONDS
SET NUMSECS=%usrInput%
GOTO :EOF

:SET_DELAYSECS
IF %INPT_TIME%==0 GOTO :EOF
SET /A delaysecs=(60*60*NUMHOURS)+(60*NUMMINUTES)+NUMSECS
GOTO :EOF


:GET_ACTION
IF %INPT_ACTION%==0 GOTO :EOF
CALL :JUST_HEADER
%DEBUGN%ECHO What would you like the computer to do once
%DEBUGN%ECHO the timer reaches zero?
%DEBUGN%ECHO.
%DEBUGN%ECHO Alert Only      [a]
%DEBUGN%ECHO Shutdown        [s]
%DEBUGN%ECHO Restart         [r]
%DEBUGN%ECHO Log Off         [l]
%DEBUGN%ECHO StandBy         [b]
%DEBUGN%ECHO Hibernate       [h]
%DEBUGN%ECHO Reload          [g]
REM %DEBUGN%ECHO Start a Program [2add]
%DEBUGN%ECHO Kill a Program  [k]
%DEBUGN%ECHO.
SET /P usrInput=[a/s/r/l/b/h/g/p/k] 
IF "%usrInput%"=="a" GOTO :EOF
IF "%usrInput%"=="s" GOTO :EOF
IF "%usrInput%"=="r" GOTO :EOF
IF "%usrInput%"=="l" GOTO :EOF
IF "%usrInput%"=="b" GOTO :EOF
IF "%usrInput%"=="h" GOTO :EOF
IF "%usrInput%"=="g" GOTO :EOF
REM IF "%usrInput%"=="p" GOTO :EOF
IF "%usrInput%"=="k" GOTO :EOF
GOTO :GET_ACTION

:PROCESS_ACTION
CALL :JUST_HEADER
%DEBUGN%ECHO Processing...
SET STime=-t 00
IF %INPT_ACTION%==1 SET actn=%usrInput%
IF %actn%==a SET Action=Alert Only
IF %actn%==s SET Action=Shutdown
IF %actn%==r SET Action=Reboot
IF %actn%==l SET Action=Log Off
IF %actn%==b SET Action=StandBy
IF %actn%==h SET Action=Hibernate
IF %actn%==h SET STime=
IF %actn%==g SET Action=Reload (Reboots and reloads your open programs)
IF %actn%==p CALL :GET_PROGRAM
IF %actn%==p SET Action=Start %ProgramName%
IF %actn%==k CALL :GET_PROGRAM
IF %actn%==k SET Action=Kill %ProgramName%
GOTO :EOF


:GET_PROGRAM
IF %INPT_ACTION%==0 GOTO :EOF
CALL :CALC_NAMES
CALL :JUST_HEADER
IF %actn%==p SET ACT=start
IF %actn%==k SET ACT=kill
%DEBUGN%ECHO What program would you like to %ACT%
%DEBUGN%ECHO once the timer reaches zero?
%DEBUGN%ECHO.
SET PROGRAM=
CALL :SHOW_CUSTOM_PROGRAMS
SET usrInput=
SET /P usrInput=[] 
IF NOT "%usrInput%"=="" SET PROGRAM=%usrInput%
IF /I "%usrInput%"=="1" SET PROGRAM=%CUSTOMPROGRAM1%
IF /I "%usrInput%"=="2" SET PROGRAM=%CUSTOMPROGRAM2%
IF /I "%usrInput%"=="3" SET PROGRAM=%CUSTOMPROGRAM3%
IF /I "%usrInput%"=="4" SET PROGRAM=%CUSTOMPROGRAM4%
IF "%PROGRAM%"=="" GOTO :GET_PROGRAM
GOTO :EOF



:PERFORM_ACTION
%DEBUGN%ECHO.
%DEBUGN%ECHO.
%DEBUGN%ECHO Program is now performing action specified...
IF NOT "%DEBUGGING%"=="0" CALL :SLEEP 3
REM Switch User: tsdiscon.exe
IF %actn%==a GOTO :ALERTING
IF %actn%==p GOTO :START_PROGRAM
IF %actn%==k GOTO :KILL_PROGRAM
CALL :DISABLEHIBERNATION
CALL :STANDBY
%DEBUGN%IF NOT %actn%==b SHUTDOWN -%actn% -f %STime%
%DEBUGN%IF NOT %actn%==b EXIT
GOTO :ENABLEHIBERNATION


:ALERTING
%DEBUGN%ECHO.
%DEBUGN%ECHO ALERT: Timer Done!
%DEBUGN%ECHO.
%DEBUGN%ECHO Press any key to start over...
PAUSE>NUL
GOTO :EOF

:START_PROGRAM

GOTO :EOF



:KILL_PROGRAM
%DEBUGN%ECHO Checking for %PROGRAM%...
TASKLIST | FIND "%PROGRAM%">NUL
IF ERRORLEVEL 1 GOTO :KILLFAILED
%DEBUGN%ECHO Killing %PROGRAM%...
SET ATTEMPTS=0
:TRYKILLPROCESS
SET /A ATTEMPTS+=1
IF %ATTEMPTS% GEQ 6 GOTO :KILLFAILED
TASKKILL /F /T /IM "%PROGRAM%"
IF ERRORLEVEL 1 GOTO :TRYKILLPROCESS
%DEBUGN%ECHO.
%DEBUGN%ECHO Press any key to start over...
PAUSE>NUL
GOTO :EOF

:KILLFAILED
CALL :STATS
%DEBUGN%ECHO Could not kill "%PROGRAM%"
%DEBUGN%ECHO Please make sure you spelled the name 
%DEBUGN%ECHO correctly and have sufficient permissions.
%DEBUGN%ECHO.
%DEBUGN%ECHO Press any key to start over...
PAUSE>NUL
GOTO :EOF



:DISABLEHIBERNATION
SET HB=0
IF EXIST %SystemDrive%\hiberfil.sys SET HB=1
IF %actn%==b IF %HB%==0 ECHO Hibernation already disabled...
IF %actn%==b IF %HB%==1 ECHO Temporarily disabling hibernation...
%DEBUGN%IF %actn%==b IF %HB%==1 powercfg -hibernate off
GOTO :EOF


:STANDBY
REM Rundll32.exe powrprof.dll,SetSuspendStateLock
REM %DEBUGN%IF %actn%==b rundll32.exe powrprof.dll,SetSuspendState 0,1,0
%DEBUGN%IF %actn%==b Rundll32.exe powrprof.dll,SetSuspendState Standby
GOTO :EOF


:ENABLEHIBERNATION
IF %actn%==b IF %HB%==1 ECHO Re-enabling hibernation...
%DEBUGN%IF %actn%==b IF %HB%==1 powercfg -hibernate on
GOTO :EOF




:WAITSECS

REM -----------------------PROGRAM TIMER--------------------------

REM ******INITIALIZE TIMER*****
IF "%1"=="" SET %1=10
IF "%1"=="0" GOTO :EOF

REM Calculate total time
SET /A PINGS="%1/TIMER_REFRESH_RATE"
SET /A TTLSCNDS="PINGS*TIMER_REFRESH_RATE"
SET /A HOURS="(TTLSCNDS/3600)"
SET /A MINUTES2="(TTLSCNDS-(HOURS*3600))/60"
SET /A SECONDS="TTLSCNDS-((HOURS*3600)+(MINUTES2*60))"

REM Set HOURS to "" or "##:"
REM (always 2 digits or nothing)
IF %HOURS%==0 (
	SET HOURS=
) ELSE (
	SET HOURS=%HOURS%:
)

REM Set MINUTES2 to "","##:", "00:", or "0#:"
REM (always 2 digits or nothing)
REM (always 2 digits if HOURS>0)
IF %MINUTES2%==0 (
	IF "%HOURS%"=="" (
		SET MINUTES2=
	) ELSE (
		SET MINUTES2=00:
	)
) ELSE (
	SET MINUTES2=%MINUTES2%:
)

REM Set SECONDS to "##" or "0#"
REM (always 2 digits)
IF %SECONDS% LEQ 9 SET SECONDS=0%SECONDS%

REM Set Ticker to 0
SET ticker=0

REM ****END INITIALIZE TIMER***


:WAITING
REM *****TICKING TIMER*****


REM Calculate REM aining time
SET /A left="PINGS-ticker"
SET /A ttlscndslft="left*TIMER_REFRESH_RATE"
SET /A hrs="ttlscndslft/3600"
SET /A mins="(ttlscndslft-(hrs*3600))/60"
SET /A scnds="(ttlscndslft-((hrs*3600)+(mins*60)))"

REM Bump ticker
SET /A ticker+=1


REM Set hrs to "","##:", or "0#:"
REM (always 2 digits or nothing)
IF %hrs%==0 (
	SET hrs=
) ELSE (
	SET hrs=%hrs%:
)

REM Set mins to "","##:", "00:", or "0#:"
REM (always 2 digits or nothing)
REM (always 2 digits if HOURS>0)
IF %mins%==0 (
	IF "%hrs%"=="" (
		SET mins=
	) ELSE (
		SET mins=00:
	)
) ELSE (
	IF "%hrs%"=="" (
		SET mins=%mins%:
	) ELSE (
		IF %mins% LEQ 9 (
			SET mins=0%mins%:
		) ELSE (
			SET mins=%mins%:
		)
	)
)

REM Set SECONDS to "##" or "0#"
REM (always 2 digits)
IF %scnds% LEQ 9 SET scnds=0%scnds%

REM Update TimeStamp
SET TimerStatus=Time Left:  %hrs%%mins%%scnds% of %HOURS%%MINUTES2%%SECONDS%

REM Display updated TimeStamp
SET isWaiting=0
CALL :STATS
CALL :SLEEP %TIMER_REFRESH_RATE%

REM Cycle through "WAITING" again if waiting time 
REM has not been reached
IF %ticker% LEQ %PINGS% GOTO :WAITING
GOTO :EOF
REM ---------------------END PROGRAM TIMER------------------------


:TEST_USRINPUT_NUM
SET num=%1
IF "%num%"=="" SET num=0
IF "%num%"=="0" SET %2=0
IF "%num%"=="0" GOTO :EOF

SET /a num=%1
IF "%num%"=="0" SET num=INVALID
SET %2=%num%
GOTO :EOF